package dviz.test.vo;

import java.util.HashMap;

import com.jme.math.Matrix3f;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.SharedNode;

import dviz.tools.ModelRepository;
import dviz.visualSystem.impl.AnimationObject;


/**
 * @author  zxq071000
 */
public class SimpleCone extends AnimationObject {

	private float x;
	private float y;
	private float speedx;
	private float speedy;
	private float size;

	public SimpleCone(HashMap<String, Object> initVector) {
		super(initVector);

		ModelRepository modelRepository = ModelRepository.get();
		SharedNode node = new SharedNode((Node) modelRepository.getRepository()
				.get("cone"));
		// Sphere node = new Sphere("", new Vector3f(), 10, 10, 10);
		node.setLocalRotation(new Quaternion(new float[] {
				(float) Math.PI / 2f, 0, 0 }));
		node.setLocalScale(0.2f);
		getRenderNode().attachChild(node);
	}

	/**
	 * @param size
	 * @uml.property  name="size"
	 */
	public void setSize(float size) {
		this.size = size;
		getRenderNode().setLocalScale(size);
	}

	/**
	 * @param x
	 * @uml.property  name="x"
	 */
	public void setX(float x) {
		this.x = x;
		getRenderNode().getLocalTranslation().setX(x);
	}

	/**
	 * @param y
	 * @uml.property  name="y"
	 */
	public void setY(float y) {
		this.y = y;
		getRenderNode().getLocalTranslation().setZ(y);
	}

	/**
	 * @param speedx
	 * @uml.property  name="speedx"
	 */
	public void setSpeedx(float speedx) {
		this.speedx = speedx;
		updateDirection();

	}

	/**
	 * @param speedy
	 * @uml.property  name="speedy"
	 */
	public void setSpeedy(float speedy) {
		this.speedy = speedy;
		updateDirection();
	}

	private void updateDirection() {
		float x = speedx;
		float y = speedy;
		float len = (float) Math.sqrt(x * x + y * y);
		if (len < Float.MIN_VALUE)
			return;
		x /= len;
		y /= len;
		Matrix3f rot = new Matrix3f();
		Vector3f vY = new Vector3f(0, 1, 0);
		Vector3f vX = new Vector3f(y, 0, -x);
		Vector3f vZ = new Vector3f(x, 0, y);
		rot.fromAxes(vX, vY, vZ);
		getRenderNode().setLocalRotation(rot);
	}

}
